layui.define('conf', function(exports){
	var conf = layui.conf;
	
	utils = {
		parseRouter : function(href){
			href = /^[\/#\.]|^http/.test(href) ? href : "/" + href
			return href.replace(/(\/+)/g, "/");
		},
		sessionData : function(o, value){
			if(typeof(o) === 'object'){
				for(i in o){
					utils.sessionData(i, o[i]);
				}
				return;
			}
			if(utils.isBlank(value)){
				return layui.sessionData(conf.tableName)[o];
			}
			layui.sessionData(conf.tableName, {
				key : o,
				value : value
			})
		},
		data : function(o, value){
			if(typeof(o) === 'object'){
				for(i in o){
					utils.data(i, o[i]);
				}
				return;
			}
			if(utils.isBlank(value)){
				return layui.data(conf.tableName)[o];
			}
			layui.data(conf.tableName, {
				key : o,
				value : value
			})
		},
		//判断对象是否为空
		isBlank :function(o){
			if(o === 0){
				return false;
			}
			if(o === '' || o == null || o  === 'undefined'){
				return true;
			}
			return false;
		},
		//字符串转16进制
		stringToHex : function(str){
			if(utils.isBlank(str)){
				return "";
			}
			var hexCharCode = [];
			for(var i = 0; i < str.length; i++) {
				hexCharCode.push((str.charCodeAt(i)).toString(16));
			}
			return hexCharCode.join("");
		},
		//16进制转字符串
		hexToString : function(hexCharCodeStr){
			if(utils.isBlank(hexCharCodeStr)){
				return "";
			}
			var trimedStr = hexCharCodeStr.trim();
			var rawStr = trimedStr.substr(0, 2).toLowerCase() === "0x" ? trimedStr.substr(2) : trimedStr;
			var len = rawStr.length;
			if (len % 2 !== 0) {
				return "";
			}
			var curCharCode;
			var resultStr = [];
			for (var i = 0; i < len; i = i + 2) {
				curCharCode = parseInt(rawStr.substr(i, 2), 16); // ASCII Code Value
				resultStr.push(String.fromCharCode(curCharCode));
			}
			return resultStr.join("");
		},
		
		//给URL加上参数
		addUrlPara : function(key, value, url){
			url = url || window.location.href;
			var reg = new RegExp("(^|)"+ key +"=([^&]*)(|$)");
			var tmp = key + "=" + value;
			if(url.match(reg) != null){
				return url.replace(eval(reg),tmp);
			}else{
				return url + (url.match("[\?]") ? "&" : "?") + tmp
			}
		},
		
		getUrlPara : function(key, url){
			url = url || layui.router().href;
			if(utils.isBlank(url)){
				return null;
			}
			var reg = new RegExp("[^\?&]?" + encodeURI(key) + "=[^&$]+");
			var r = url.match(reg);
			if (r != null)
				return decodeURI(r[0].substring(r[0].search("=") + 1));
			return null;
		},
		
		getDictData : function(data, field){
			try{
				return data[field] || eval('data.'+field);
			}catch(e){
				
			}
		},
		isJsonStr : function(str){
			try {
				var obj=JSON.parse(str);
	            if(typeof obj == 'object' && obj ){
	                return true;
	            }else{
	                return false;
	            }
	        } catch(e) {
	            return false;
	        }
		},
		toJson : function(str){
			return JSON.parse(str)
		},
		getCurDate : function(fmat){
			fmat = fmat ? fmat : "YYYY-MM-DD"
			return utils.formatDateTime(new Date(), fmat)
		},
		formatSeconds : function(seconds){			
			if(!seconds || seconds < 0){
				return "";
			}
			seconds = parseInt(seconds);
			var theTime = seconds;// 秒
		    var theTimeMinute = 0;// 分
		    var theTimeHour = 0;// 小时
		    var theTimeDay = 0;
		    if(theTime >= 60) {
		    	theTimeMinute = parseInt(theTime / 60);
		        theTime = theTime % 60;
		            if(theTimeMinute >= 60) {
		            	theTimeHour = parseInt(theTimeMinute / 60);
		            	theTimeMinute = theTimeMinute % 60;	
		            	if(theTimeHour >= 24){
		            		theTimeDay = parseInt(theTimeHour / 24);
		            		theTimeHour = theTimeHour % 24;
		            	}
		            		            	
		            }
		    }
	        var result = "";
	        if(theTime > 0){
	        	result = theTime + "秒";
	        }
	        if(theTimeMinute > 0) {
	        	result = theTimeMinute + "分" + result;
	        }
	        if(theTimeHour > 0) {
	        	result = theTimeHour + "时" + result;
	        }
	        if(theTimeDay > 0) {
	        	result = theTimeDay + "天" + result;
	        }
		    return result;
		},
		formatDateTime :function(time, fmat) {
		    if (!time) {
		        return '';
		    }
		    fmat = fmat ? fmat : "YYYY-MM-DD HH:mm:ss";
		    if(typeof time === 'number' || typeof time === 'string'){
		    	return utils.formatDateTime(new Date(parseInt(time)), fmat)
		    }
		    return (time).format(fmat);
		},
		// 获取日期范围显示   
		getWeekDateRange : function(_year,_week){   
			//每年的1月1号为第一周第一天，此后7天为第一周。
		    if(_year == null || _year == '' || _week == null || _week == ''){   
		        return "";   
		    }   
		    var yearFirstDate = new Date(_year, 0 ,1);
		    var weekBeginTimeStamp =  yearFirstDate.valueOf() + (_week - 1) * 7 * 24 * 3600000;//第几周为当年1月1号加上7天的毫秒数
		    var weekEndTimepStamp = weekBeginTimeStamp + 6 * 24 * 3600000
		    var fmat = "YYYY-MM-DD";
		    return utils.formatDateTime(weekBeginTimeStamp, fmat) + " 至 "+ utils.formatDateTime(weekEndTimepStamp, fmat);   
		}
		
	}		
		
	Date.prototype.format = function(format){
        var o = {
            "M+" : this.getMonth()+1, //month
            "DD+" : this.getDate(), //day
            "HH+" : this.getHours(), //hour
            "m+" : this.getMinutes(), //minute
            "s+" : this.getSeconds(), //second
            "q+" : Math.floor((this.getMonth()+3)/3), //quarter
            "S" : this.getMilliseconds() //millisecond
        }
        if(/(Y+)/.test(format)){
            format=format.replace(RegExp.$1,(this.getFullYear()+"").substr(4 - RegExp.$1.length));
        }
        for(var k in o){
            if(new RegExp("("+ k +")").test(format)){
                format = format.replace(RegExp.$1,RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
            }
        }
        return format;
    }
	exports("utils", utils)
});